<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>html5webstorage.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_storage_mechanism_html5webstorage.js.html">html5webstorage.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2011 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Base class that implements functionality common
<a name="line17"></a> * across both session and local web storage mechanisms.
<a name="line18"></a> *
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.storage.mechanism.HTML5WebStorage&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line24"></a>goog.require(&#39;goog.iter.Iterator&#39;);
<a name="line25"></a>goog.require(&#39;goog.iter.StopIteration&#39;);
<a name="line26"></a>goog.require(&#39;goog.storage.mechanism.ErrorCode&#39;);
<a name="line27"></a>goog.require(&#39;goog.storage.mechanism.IterableMechanism&#39;);
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>/**
<a name="line32"></a> * Provides a storage mechanism that uses HTML5 Web storage.
<a name="line33"></a> *
<a name="line34"></a> * @param {Storage} storage The Web storage object.
<a name="line35"></a> * @constructor
<a name="line36"></a> * @extends {goog.storage.mechanism.IterableMechanism}
<a name="line37"></a> */
<a name="line38"></a>goog.storage.mechanism.HTML5WebStorage = function(storage) {
<a name="line39"></a>  goog.base(this);
<a name="line40"></a>  this.storage_ = storage;
<a name="line41"></a>};
<a name="line42"></a>goog.inherits(goog.storage.mechanism.HTML5WebStorage,
<a name="line43"></a>              goog.storage.mechanism.IterableMechanism);
<a name="line44"></a>
<a name="line45"></a>
<a name="line46"></a>/**
<a name="line47"></a> * The key used to check if the storage instance is available.
<a name="line48"></a> * @type {string}
<a name="line49"></a> * @const
<a name="line50"></a> * @private
<a name="line51"></a> */
<a name="line52"></a>goog.storage.mechanism.HTML5WebStorage.STORAGE_AVAILABLE_KEY_ = &#39;__sak&#39;;
<a name="line53"></a>
<a name="line54"></a>
<a name="line55"></a>/**
<a name="line56"></a> * The web storage object (window.localStorage or window.sessionStorage).
<a name="line57"></a> *
<a name="line58"></a> * @type {Storage}
<a name="line59"></a> * @private
<a name="line60"></a> */
<a name="line61"></a>goog.storage.mechanism.HTML5WebStorage.prototype.storage_;
<a name="line62"></a>
<a name="line63"></a>
<a name="line64"></a>/**
<a name="line65"></a> * Determines whether or not the mechanism is available.
<a name="line66"></a> * It works only if the provided web storage object exists and is enabled.
<a name="line67"></a> *
<a name="line68"></a> * @return {boolean} True if the mechanism is available.
<a name="line69"></a> */
<a name="line70"></a>goog.storage.mechanism.HTML5WebStorage.prototype.isAvailable = function() {
<a name="line71"></a>  if (!this.storage_) {
<a name="line72"></a>    return false;
<a name="line73"></a>  }
<a name="line74"></a>  /** @preserveTry */
<a name="line75"></a>  try {
<a name="line76"></a>    // setItem will throw an exception if we cannot access WebStorage (e.g.,
<a name="line77"></a>    // Safari in private mode).
<a name="line78"></a>    this.storage_.setItem(
<a name="line79"></a>        goog.storage.mechanism.HTML5WebStorage.STORAGE_AVAILABLE_KEY_, &#39;1&#39;);
<a name="line80"></a>    this.storage_.removeItem(
<a name="line81"></a>        goog.storage.mechanism.HTML5WebStorage.STORAGE_AVAILABLE_KEY_);
<a name="line82"></a>    return true;
<a name="line83"></a>  } catch (e) {
<a name="line84"></a>    return false;
<a name="line85"></a>  }
<a name="line86"></a>};
<a name="line87"></a>
<a name="line88"></a>
<a name="line89"></a>/** @override */
<a name="line90"></a>goog.storage.mechanism.HTML5WebStorage.prototype.set = function(key, value) {
<a name="line91"></a>  /** @preserveTry */
<a name="line92"></a>  try {
<a name="line93"></a>    // May throw an exception if storage quota is exceeded.
<a name="line94"></a>    this.storage_.setItem(key, value);
<a name="line95"></a>  } catch (e) {
<a name="line96"></a>    // In Safari Private mode, conforming to the W3C spec, invoking
<a name="line97"></a>    // Storage.prototype.setItem will allways throw a QUOTA_EXCEEDED_ERR
<a name="line98"></a>    // exception.  Since it&#39;s impossible to verify if we&#39;re in private browsing
<a name="line99"></a>    // mode, we throw a different exception if the storage is empty.
<a name="line100"></a>    if (this.storage_.length == 0) {
<a name="line101"></a>      throw goog.storage.mechanism.ErrorCode.STORAGE_DISABLED;
<a name="line102"></a>    } else {
<a name="line103"></a>      throw goog.storage.mechanism.ErrorCode.QUOTA_EXCEEDED;
<a name="line104"></a>    }
<a name="line105"></a>  }
<a name="line106"></a>};
<a name="line107"></a>
<a name="line108"></a>
<a name="line109"></a>/** @override */
<a name="line110"></a>goog.storage.mechanism.HTML5WebStorage.prototype.get = function(key) {
<a name="line111"></a>  // According to W3C specs, values can be of any type. Since we only save
<a name="line112"></a>  // strings, any other type is a storage error. If we returned nulls for
<a name="line113"></a>  // such keys, i.e., treated them as non-existent, this would lead to a
<a name="line114"></a>  // paradox where a key exists, but it does not when it is retrieved.
<a name="line115"></a>  // http://www.w3.org/TR/2009/WD-webstorage-20091029/#the-storage-interface
<a name="line116"></a>  var value = this.storage_.getItem(key);
<a name="line117"></a>  if (!goog.isString(value) &amp;&amp; !goog.isNull(value)) {
<a name="line118"></a>    throw goog.storage.mechanism.ErrorCode.INVALID_VALUE;
<a name="line119"></a>  }
<a name="line120"></a>  return value;
<a name="line121"></a>};
<a name="line122"></a>
<a name="line123"></a>
<a name="line124"></a>/** @override */
<a name="line125"></a>goog.storage.mechanism.HTML5WebStorage.prototype.remove = function(key) {
<a name="line126"></a>  this.storage_.removeItem(key);
<a name="line127"></a>};
<a name="line128"></a>
<a name="line129"></a>
<a name="line130"></a>/** @override */
<a name="line131"></a>goog.storage.mechanism.HTML5WebStorage.prototype.getCount = function() {
<a name="line132"></a>  return this.storage_.length;
<a name="line133"></a>};
<a name="line134"></a>
<a name="line135"></a>
<a name="line136"></a>/** @override */
<a name="line137"></a>goog.storage.mechanism.HTML5WebStorage.prototype.__iterator__ = function(
<a name="line138"></a>    opt_keys) {
<a name="line139"></a>  var i = 0;
<a name="line140"></a>  var storage = this.storage_;
<a name="line141"></a>  var newIter = new goog.iter.Iterator();
<a name="line142"></a>  newIter.next = function() {
<a name="line143"></a>    if (i &gt;= storage.length) {
<a name="line144"></a>      throw goog.iter.StopIteration;
<a name="line145"></a>    }
<a name="line146"></a>    var key = goog.asserts.assertString(storage.key(i++));
<a name="line147"></a>    if (opt_keys) {
<a name="line148"></a>      return key;
<a name="line149"></a>    }
<a name="line150"></a>    var value = storage.getItem(key);
<a name="line151"></a>    // The value must exist and be a string, otherwise it is a storage error.
<a name="line152"></a>    if (!goog.isString(value)) {
<a name="line153"></a>      throw goog.storage.mechanism.ErrorCode.INVALID_VALUE;
<a name="line154"></a>    }
<a name="line155"></a>    return value;
<a name="line156"></a>  };
<a name="line157"></a>  return newIter;
<a name="line158"></a>};
<a name="line159"></a>
<a name="line160"></a>
<a name="line161"></a>/** @override */
<a name="line162"></a>goog.storage.mechanism.HTML5WebStorage.prototype.clear = function() {
<a name="line163"></a>  this.storage_.clear();
<a name="line164"></a>};
<a name="line165"></a>
<a name="line166"></a>
<a name="line167"></a>/**
<a name="line168"></a> * Gets the key for a given key index. If an index outside of
<a name="line169"></a> * [0..this.getCount()) is specified, this function returns null.
<a name="line170"></a> * @param {number} index A key index.
<a name="line171"></a> * @return {?string} A storage key, or null if the specified index is out of
<a name="line172"></a> *     range.
<a name="line173"></a> */
<a name="line174"></a>goog.storage.mechanism.HTML5WebStorage.prototype.key = function(index) {
<a name="line175"></a>  return this.storage_.key(index);
<a name="line176"></a>};
</pre>


</body>
</html>
